<template>
    <div class="order-edit">
        <h2>编辑订单</h2>
        <el-form :model="editFormData">
            <el-form-item label="订单号">
                <el-input v-model="editFormData.orderNo" />
            </el-form-item>
            <el-form-item label="下单时间">
                <el-input v-model="editFormData.orderTime" />
            </el-form-item>
            <el-form-item label="联系电话">
                <el-input v-model="editFormData.phone" />
            </el-form-item>
            <el-form-item label="收货人">
                <el-input v-model="editFormData.consignee" />
            </el-form-item>
            <el-form-item label="送货地址">
                <el-input v-model="editFormData.deliverAddress" />
            </el-form-item>
            <el-form-item label="送达时间">
                <el-input v-model="editFormData.deliveryTime" />
            </el-form-item>
            <el-form-item label="备注">
                <el-input v-model="editFormData.remarks" />
            </el-form-item>
            <el-form-item label="订单金额">
                <el-input v-model="editFormData.orderAmount" />
            </el-form-item>
            <el-form-item label="订单状态">
                <el-select v-model="editFormData.orderState">
                    <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </el-form-item>
        </el-form>
        <el-button type="primary" @click="submitEditForm">保存</el-button>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { editOrder } from '../../api/account';
import { detailOrder } from '../../api/account';

const options = [
    { value: '已完成', label: '已完成' },
    { value: '未完成', label: '未完成' },
    { value: '已取消', label: '已取消' },
];

const route = useRoute();
const router = useRouter();
const editFormData = ref({});
const id = route.params.id;

const fetchOrderData = async () => {
    const id = route.params.id;
    try {
        // 这里假设获取订单详情接口和之前查看详情接口一致
        const response = await detailOrder(id);
        editFormData.value = { ...response.data };
    } catch (error) {
        console.error('Error fetching order data for edit:', error);
    }
};

const submitEditForm = async () => {
    try {
        const response = await editOrder(editFormData.value);
        if (response.code === 0) {
            ElMessage.success('修改订单成功!');
            router.push('/orderList'); // 修改成功后跳回订单列表页面
        } else {
            ElMessage.error(response.msg);
        }
    } catch (error) {
        console.error('Error editing order:', error);
    }
};

onMounted(() => {
    fetchOrderData();
});
</script>

<style lang="scss" scoped>
.order-edit {
    padding: 20px;
    background-color: #fff;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}
</style>